package com.lin.mr.maizilaundry.fragmentmain;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.lin.mr.maizilaundry.R;
import com.lin.mr.maizilaundry.activity.OrderActivity;
import com.lin.mr.maizilaundry.activity.RegisterActivity;
import com.lin.mr.maizilaundry.activity.ShareActivity;
import com.lin.mr.maizilaundry.myadapter.FragmentMeAdapter;
import com.lin.mr.maizilaundry.myapplication.MyApplication;
import com.lin.mr.maizilaundry.utils.CountEvent;
import com.lin.mr.maizilaundry.utils.ShareUtils;

import de.greenrobot.event.EventBus;


/**
 * 这里的用户体验理想的效果应该是这样的，用户看到这个Fragment时上面的用户名和手机号码就已经写好了，
 * 如果只在setUserVisibleHint中setText的话会有滞后现象，所以我在onCreateView中也调用getUserInfo方法，
 * 让Fragment在预加载的时候就调用SetText方法。但是一定要注意对TextView初始化，否则会有空指针。
 *
 */
public class FragmentMe extends Fragment {
    private TextView tv_name_me, tv_phone_me, tv_recommend;
    private GridView gridView;
    private View view;
    private boolean isCreateView ;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        isCreateView = true;
        initView();
        getUserInfo();
        Log.e("Tag", "onCreateView中的getUserInfo()");
        tv_recommend.setText(Html.fromHtml("推荐码: <b>SQ6RHJ</b>"));//将推荐码设置为粗体

        gridView.setAdapter(new FragmentMeAdapter(getActivity()));
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            //GridView中的各个Item设置点击事件
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0: //我的订单
                        startActivity(new Intent(getActivity(), OrderActivity.class));
                        break;
                    case 1:
                        break;
                    case 2: //我的地址
                        startActivity(new Intent(getActivity(), RegisterActivity.class));
                        break;
                    case 3:
                        startActivity(new Intent(getActivity(), ShareActivity.class));
                        break;
                    case 4:
                        break;
                    default:
                        break;
                }
            }
        });

        //让用户可以点击名字登录或者修改信息
        tv_name_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), RegisterActivity.class);
                startActivity(intent);
            }
        });
        return view;
    }


    //setUserVisibleHint表示在fragment可見時調用
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser) {
            if (isCreateView==false){
                initView();
            }
//            Log.e("Tag", "setUserVisibleHint中的getUserInfo()");
            getUserInfo();
        }
    }

    //初始化控件
    public void initView() {
        view = View.inflate(MyApplication.getContext(), R.layout.fragment_me, null);
        tv_name_me = (TextView) view.findViewById(R.id.tv_name_me);
        tv_phone_me = (TextView) view.findViewById(R.id.tv_phone_me);
        tv_recommend = (TextView) view.findViewById(R.id.tv_recommend);
        gridView = (GridView) view.findViewById(R.id.fragment_me_gridView);
    }

    public void getUserInfo() {
        Log.e("Tag","getUserInfo被调用了");
        String userName = ShareUtils.getName(MyApplication.getContext());
        String userPhone = ShareUtils.getNumber(MyApplication.getContext());
        if (userName.equals("") && userPhone.equals("")){
            tv_name_me.setText("请点击输入用户名");
            tv_phone_me.setText("手机号码");
        }else if(userName.equals("") && !userPhone.equals("")){
            tv_name_me.setText("请点击输入用户名");
            tv_phone_me.setText(userPhone);
        }else if (!userName.equals("") && !userPhone.equals("")){ //这里的if判断语句要写全
            tv_name_me.setText(userName);
            tv_phone_me.setText(userPhone);
//            Log.e("Tag","TTT"+tv_name_me.getText()+","+userPhone);
        }
    }

    public void onEventMainThread(CountEvent event) {
        int count = event.getCount();
        if (count == 5) {
            initView();
            getUserInfo();
//            Log.e("Tag", "FragmentMe中的EventBus");
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
//        Log.e("Tag", "onCreate中的getUserInfo()");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isCreateView = false;
    }
}
